﻿using AutoMapper;
using HamcoWcs.Contract;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractPcon;
using HamcoWcs.DAL;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.Devices;
using HamcoWcs.Service.Common.SingalRs;
using Microsoft.AspNetCore.SignalR;

namespace HamcoWcs.Service.Common
{
    [AutoInject(typeof(IModeService), InjectType.Scope)]

    public class ModeService : ScopeServiceBase, IModeService
    {

        private readonly ILoggerService _loggerService;
        private readonly IBaseConfigService _baseConfigService;
        private readonly IPathService _pathService;
        private readonly IHubContext<PushHub> _pushContext;
        private readonly IPconBaseService _pconBaseService;
        private readonly IDbMsgService _dbMsgService;


        public ModeService(WcsDbContext wcsDbContext,
                           IMapper mapper,
                           ILoggerService loggerService,
                           IBaseConfigService baseConfigService,
                           IPathService pathService,
                           IHubContext<PushHub> pushContext,
                           IPconBaseService pconBaseService,
                           IDbMsgService dbMsgService)
            : base(wcsDbContext, mapper)
        {
            _loggerService = loggerService;
            _baseConfigService = baseConfigService;
            _pathService = pathService;
            _pushContext = pushContext;
            _pconBaseService = pconBaseService;
            _dbMsgService = dbMsgService;
        }

        //出库模式，由前端改为waitIn或者waitOut状态，worker不停地轮寻并检测
        public void ChangeModeToOut(PconDto pconDto)
        {
            //if (pconDto.IsGoods == 1) return;
            //var wmsName = _pathService.GetWcsNode(pconDto.PconName.ToString()).WmsNodeName;
            //var configKey = wmsName + "_mode";

            //var existTask = _wcsDbContext.SubTasks.Any(x => x.FromLoc == pconDto.PconName.ToString());

            //if (existTask) return; // 存在任务不执行

            //if (pconDto.WorkType != 2)
            //{
            //    var plc = _pconBaseService.GetPconPlc(pconDto.PlcName);

            //    var pconSendMsg = new PconSendMsg()
            //    {
            //        PconName = (ushort)pconDto.PconName,
            //        Request = 7
            //    };
            //    _dbMsgService.IsRepeated(pconDto, pconSendMsg);
            //    return;
            //}
            //var iOPort = EnumMode.Out.ToString();
            //_baseConfigService.SetConfigMsg(configKey, iOPort);
            ////await _pushContext.Clients.All.SendAsync("PushModeStatus", iOPort);
            ////_loggerService.AddWcsRunLog($"{pconDto.PconName}", "切换出库模式成功");
        }
        public void ChangeModeToIn(PconDto pconDto)//入库模式
        {
            //if (pconDto.IsGoods == 1) return;
            //var wmsName = _pathService.GetWcsNode(pconDto.PconName.ToString()).WmsNodeName;
            //var configKey = wmsName + "_mode";
            //var taskEntity = _wcsDbContext.SubTasks.Include(x => x.MainTask).AsNoTracking().FirstOrDefault(x => x.FromLoc == pconDto.PconName.ToString());
            //if (taskEntity != null) return;
            //if (pconDto.WorkType != 1)
            //{
            //    var pconSendMsg = new PconSendMsg()
            //    {
            //        PconName = (ushort)pconDto.PconName,
            //        Request = 6
            //    };
            //    _dbMsgService.IsRepeated(pconDto, pconSendMsg);
            //    return;
            //}
            //var iOPort = EnumMode.In.ToString();
            //_baseConfigService.SetConfigMsg(configKey, iOPort);
            //_loggerService.AddWcsRunLog($"{pconDto.PconName}", "切换入库模式成功");

            ////if (taskEntity != null && IOPort.ConfigValue == EnumMode.Out.ToString())
            ////{
            ////    _loggerService.AddWcsRunLog($"{taskEntity.MainTask.UnitId}", "切换入库模式失败，当前有该站点的出库任务");
            ////    IOPort.ConfigValue = EnumMode.WaitIn.ToString();
            ////    _wcsDbContext.Update(IOPort);
            ////    _wcsDbContext.SaveChanges();
            ////    return;
            ////}
            ////if (pconDto.IsGoods == 1)
            ////{
            ////    _loggerService.AddWcsRunLog($"{taskEntity.MainTask.UnitId}", "切换入库模式失败，当前有该站点仍有货物");
            ////    IOPort.ConfigValue = EnumMode.WaitIn.ToString();
            ////    _wcsDbContext.Update(IOPort);
            ////    _wcsDbContext.SaveChanges();
            ////    return;
            ////}
            ////var nodeName = _wcsDbContext.NodeInfos.AsNoTracking().FirstOrDefault(x => x.NodeName == pconDto.PconName.ToString());
            ////IOPort.ConfigValue = EnumMode.In.ToString();
            ////_loggerService.AddWcsRunLog($"{pconDto.PconName},{nodeName.WmsNodeName}", "切换入库模式成功");
            ////_wcsDbContext.Update(IOPort);
            ////_wcsDbContext.SaveChanges();
        }
        public void CheckInMode(PconDto pconDto)
        {
            //    if (pconDto.WorkType != (int)EnumMode.In)
            //    {
            //        var wmsName = _pathService.GetWcsNode(pconDto.PconName.ToString()).WmsNodeName;
            //        var configKey = wmsName + "_mode";
            //        var iOPort = EnumMode.WaitIn.ToString();
            //        _baseConfigService.SetConfigMsg(configKey, iOPort);
            //        //_loggerService.AddWmsMsgLog("WMS", "内存模式和数据库模式不一致，切换WaitIn", $"{configKey}");
            //    }
            //    return;

        }
        public void CheckOutMode(PconDto pconDto)
        {
            //if (pconDto.WorkType != (int)EnumMode.Out)
            //{
            //    var wmsName = _pathService.GetWcsNode(pconDto.PconName.ToString()).WmsNodeName;
            //    var configKey = wmsName + "_mode";
            //    var iOPort = EnumMode.WaitOut.ToString();
            //    _baseConfigService.SetConfigMsg(configKey, iOPort);
            //    //_loggerService.AddWmsMsgLog("WMS", "内存模式和数据库模式不一致，切换WaitOut", $"{configKey}");
            //}
            //return;
        }

    }
}
